<?php
/**
 * Create by 11li
 * Date 2020/1/9
 * Time 11:25
 * 购物车
 */

namespace app\api\controller\v1;

use app\common\controller\Api;
use app\common\service\CartService;

class Cart extends Api {
	
	/**
	 * 购物车列表
	 */
	public function index(){
		$obj = new CartService();
		$res = $obj->getCartByUserId($this->auth->id);
		$this->success('查询成功', $res);
	}
	
	
	/**
	 * 添加商品
	 */
	public function add() {
		$data = $this->request->post();
		$check = new \app\common\validate\Cart();
		if(!$check->check($data)) {
			$this->error($check->getError());
		}
		
		$obj = new CartService();
		$res = $obj->add($data['goods_id'], $data['number'], $data['spec_id'] ? : 0);
		if($res) {
			$this->success('添加成功', ['number' => $res]);
		} else {
			$this->error($obj->getError());
		}
	}
	
	/**
	 * 删除
	 */
	public function del(){
	
	}
	
	/**
	 * 添加商品
	 */
	public function changeNumber() {
		$id = $this->request->post('id');
		$number = $this->request->post('number');
		if(!$id || !$number) {
			$this->error('参数错误');
		}
		
		
		$model = new \app\common\model\Cart();
		$res = $model->save(['number' => $number], ['user_id' => $this->auth->id, 'id' => $id]);
		if($res !== false) {
			$this->success('更改成功');
		} else {
			$this->error('更改出错');
		}
	}
	
}
